<template>
  <div class="shopping-page">
    <div class="index-title">二手交易商城首页</div>
    <div class="search-container">
      <span class="search-label">商品名称：</span>
      <el-input v-model="params.item_name" clearable placeholder="请输入内容" class="search-main" clearavle @clear="getList" />
      <el-button type="primary" icon="el-icon-search" @click="getList">搜索</el-button>
    </div>
    <div class="cart">
      <el-button type="primary" icon="el-icon-shopping-cart-full" @click="toCart">我的购物车</el-button>
    </div>
    <div class="products">
      <div v-for="product in list" :key="product.id" class="product">
        <img :src="product.image" alt="商品图片" class="imgSet">
        <h5>商品名：{{ product.item_name }}</h5>
        <p style="font-size:18px; color:red;">价格￥：{{ product.price }}</p>
        <p style="font-size:12px;">库存量：{{ product.quantity }}</p>
        <p style="font-size:12px;">商品描述：{{ product.description }}</p>
        <el-button class="addcart-container" round type="success" icon="el-icon-shopping-cart-2" @click="addToCart(product)">加入购物车</el-button>
      </div>
    </div>

    <!-- <div class="cart">
      <h2>购物车</h2>
      <p>商品总价： {{ getTotal() }} 元</p >
      <ul>
        <li v-for="item in cart" :key="item.id">{{ item.name }} -￥ {{ item.price }}</li>
      </ul>
    </div> -->
  </div>

</template>

<script>
import { addCartAPI, getAllItemAPI } from '@/api/item'
export default {
  data() {
    return {
      list: [],
      cart: [],
      params: {
        item_name: null

      },
      item: {
        item_id: '',
        item_name: '',
        description: '', // 描述
        price: '',
        quantity: '', // 数量
        image: '',
        seller_id: ''
      }
    }
  },
  mounted() {
    this.getList()
  },
  methods: {
    toCart() {
      this.$router.push('/park/shoppingCart')
    },
    // async deleteMyItem(product) {
    //   try {
    //     this.params.item_id = product.item_id
    //     await deleteItemAPI(this.params)
    //     await this.getList()
    //     console.log(product.item_id)
    //     this.$message.success('删除成功')
    //   } catch (error) {
    //     this.$message.error(error.response.data.msg)
    //   }
    // },
    addToCart(product) {
      console.log(product.item_id)
      addCartAPI({
        item_id: product.item_id,
        quantity: product.quantity,
        price: product.price
      }).then((res) => {
        console.log(res)
        this.$message.success('添加成功')
      })
    },
    getList() {
      getAllItemAPI(this.params).then((res) => {
        this.list = res
        this.params.item_name = null
        console.log(res)
      })
    }

  }
}
</script>

<style lang="scss" scoped>
.shopping-page {
  display: flex;
  flex-direction: column;
  align-items: center;
}

.index-title {
  width: 100%;
  color: #ff4d4d;
  font-size: 36px;
  font-weight: bold;
  text-align: center;
  margin-bottom: 20px;
}

.search-container {
  display: flex;
  align-items: center;
  border-bottom: 1px solid #ededed;
  padding-bottom: 20px;

  .search-main {
    width: 320px;
    margin-right: 10px;
  }
}

.products {
  margin-left: 10px;
  display: flex;
  flex-wrap: wrap;
  justify-content: space-around;
}

.product {
  flex: 0 0 300px;
  margin: 10px;
  background-color: #f8f8f8;
  border: 1px solid #ddd;
  border-radius: 8px;
  padding: 10px;
  box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
  transition: transform 0.3s ease;

  &:hover {
    transform: scale(1.05);
  }

  img {
    width: 100%;
    height: 150px;
    object-fit: cover;
    border-radius: 8px;
  }

  h3 {
    font-size: 18px;
    margin: 10px 0;
  }

  .price {
    font-size: 16px;
    color: #e44d26;
  }

  .quantity, .description {
    font-size: 14px;
    margin: 5px 0;
  }

  .addcart-container {
    margin-top: 10px;
  }
}

.cart {
  margin-top: 20px;
}
</style>
